package com.lovo.ssc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.ssc.bean.Commodity;
import com.lovo.ssc.bean.Returnorder;
import com.lovo.ssc.bean.Returnorderhandle;
import com.lovo.ssc.service.IReturnorderService;
import com.lovo.ssc.service.IReturnorderhandleService;
import com.lovo.util.Verifier;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lovo
 * @since 2022-07-11
 */
@RestController
@RequestMapping("/returnorder")
@Api(tags = "退货应用控制器002------刘文龙")
@Slf4j
public class ReturnorderController {

	@Autowired
	IReturnorderService returnorderService;
	@Autowired
	IReturnorderhandleService handleService;

	@ApiOperation(value = "生成退货订单")
	@PostMapping("addReturnOrder/{orderCode}/{commodityCode}/{number}/{userName}/{reason}")
	public void addReturnOrder(@PathVariable("orderCode") String orderCode,
	                           @PathVariable("commodityCode") String commodityCode,
	                           @PathVariable("number") int number,
	                           @PathVariable("userName") String userName,
	                           @PathVariable("reason") String reason) {
		log.info("生成退货订单收到的数据" + orderCode + "   " + commodityCode + "   " + number+"  "+userName
		+"  "+reason);
		//生成退货单号
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		String returnOrderCode = commodityCode + timestamp.getTime();
		//得到商品对象
		Commodity commodity = returnorderService.findCommodityByCode(commodityCode);
		//生成退货订单
		Returnorder returnorder = new Returnorder();
		returnorder.setUsername(userName);
		returnorder.setReturngoodscode(returnOrderCode);
		returnorder.setOrdercode(orderCode);
		returnorder.setCommoditycode(commodityCode);
		returnorder.setCommodityname(commodity.getCommodityname());
		returnorder.setNorms(commodity.getNorms());
		returnorder.setType(commodity.getType());
		returnorder.setRetuenprice(commodity.getPrice() * number);
		returnorder.setReturnreason(reason);
		returnorder.setReturnstate("0");
		returnorder.setReturndate(String.valueOf(LocalDate.now()));
		log.info(returnorder.toString());

		//添加退货订单
		returnorderService.save(returnorder);
//		//添加退货处理记录
		Returnorderhandle handle = new Returnorderhandle();
		handle.setFkusername(returnorder.getUsername());
		handle.setFkreturngoodscode(returnorder.getReturngoodscode());
		handle.setHandlestate("0");
		handle.setHandledate(LocalDate.now()+"");
		handleService.save(handle);

		//修改购买订单状态
		handleService.updateOrderStatus(orderCode);
	}

	@ApiOperation(value ="查询退货")
	@GetMapping("findReturnOrder")
	public List<Returnorder> findReturnOrder(HttpServletRequest request) {

		String token = request.getHeader("token");
		String userName = Verifier.verifierJwt(token);

		return returnorderService.findByName(userName);
	}

}
